package org.apache.rocketmq.console.script.service.impl;

import java.util.List;

import org.apache.rocketmq.console.script.dao.EnvironDicDao;
import org.apache.rocketmq.console.script.dao.ScriptInfoDao;
import org.apache.rocketmq.console.script.dao.SysInfoDao;
import org.apache.rocketmq.console.script.model.EnvironDic;
import org.apache.rocketmq.console.script.model.ScriptInfo;
import org.apache.rocketmq.console.script.model.SysInfo;
import org.apache.rocketmq.console.script.service.ScriptService;
import org.apache.rocketmq.console.shiro.entity.UserInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class ScriptServiceImpl implements ScriptService {
	@Value("${rocketmq.config.namesrvAddr}")
	private String namesrvAddr;

	@Autowired
	private ScriptInfoDao scriptInfoDao;

	@Autowired
	private EnvironDicDao environDicDao;

	@Autowired
	private SysInfoDao sysInfoDao;

	@Override
	public List<ScriptInfo> findAllScript() {

		UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();

		EnvironDic environDic = null;
		String[] namesrvAddrList = namesrvAddr.split(";");
		for (String nameserverAddr : namesrvAddrList) {
			environDic = environDicDao.findOneByNameserverLike("%" + nameserverAddr + "%");
			if (environDic != null) {
				break;
			}
		}

		// 查询到所有的数据
		int permissionId = userInfo.getPermissionId();
		if (environDic == null || permissionId == 1) {
			List<ScriptInfo> scriptInfos = scriptInfoDao.findAll();
			for (ScriptInfo scriptInfo : scriptInfos) {
				EnvironDic environDic1 = environDicDao.findOne(scriptInfo.getEnvId());
				if (environDic1 != null) {
					scriptInfo.setEnviroCode(environDic1.getEnviroCode());
					scriptInfo.setEnviroName(environDic1.getEnviroName());
					scriptInfo.setRegionCode(environDic1.getRegionCode());
					scriptInfo.setRegionName(environDic1.getRegionName());
					scriptInfo.setNameserver(environDic1.getNameserver());
				}
			}
			return scriptInfos;
		} else {
			// 根据当前所属域的数据
			List<ScriptInfo> scriptInfos = scriptInfoDao.findByRegionCode(environDic.getRegionCode());
			for (ScriptInfo scriptInfo : scriptInfos) {
				scriptInfo.setEnviroCode(environDic.getEnviroCode());
				scriptInfo.setEnviroName(environDic.getEnviroName());
				scriptInfo.setRegionCode(environDic.getRegionCode());
				scriptInfo.setRegionName(environDic.getRegionName());
				scriptInfo.setNameserver(environDic.getNameserver());
			}
			return scriptInfos;
		}

	}

	@Override
	public List<SysInfo> findAllSysInfo() {
		List<SysInfo> findAll = sysInfoDao.findAll();
		return findAll;
	}

	@Override
	public List<EnvironDic> findAllEnvironDic() {
		return environDicDao.findAll();
	}

	@Override
	public boolean save(ScriptInfo entity) {
		try {
			scriptInfoDao.save(entity);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	@Override
	public EnvironDic findEnvironDicByRegionCodeAndEnviroCode(String regionCode, String enviroCode) {
		return environDicDao.findOneByRegionCodeAndEnviroCode(regionCode, enviroCode);
	}

	@Override
	public List<EnvironDic> findAllEnvironDicByRegionCode(String regionCode) {
		return environDicDao.findByRegionCode(regionCode);
	}

	@Override
	public ScriptInfo findScriptInfoById(int id) {

		return scriptInfoDao.findOneById(id);
	}

	@Override
	public EnvironDic findOneEnvironDic(int id) {
		return environDicDao.findOne(id);
	}

	@Override
	public String findSysNameBySysCode(String code) {
		return sysInfoDao.findOneByCode(code).getName();
	}

	@Override
	public List<ScriptInfo> findScriptInfoByRegionCode(String regionCode) {
		return scriptInfoDao.findByRegionCode(regionCode);
	}

}
